
package com.huaweicloud.haydn.delivercore.agent.service.metadata.impl;

import com.huaweicloud.haydn.delivercore.agent.common.exception.BusinessException;
import com.huaweicloud.haydn.delivercore.agent.dto.excel.FieldEmptyExcelDto;
import com.huaweicloud.haydn.delivercore.agent.utils.HiveAuthUtil;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 功能描述
 *
 * @since 2022-12-21
 */
@Slf4j
@Service("MRS-Hudi")
public class HudiDatabaseUtil extends BaseDatabaseUtil {
    @Override
    public String getSelectedDatabasesSql(String dbNames) throws BusinessException {
        return null;
    }

    @Override
    public String getAllTablesByDbSql(String dbNames) {
        return null;
    }

    @Override
    public String getSelectedTableSql(String dbNames, String tableNames) {
        return null;
    }

    @Override
    public String getAllFieldsSql() {
        return null;
    }

    @Override
    public String getAllFieldsByDbSql(String dbNames) {
        return null;
    }

    @Override
    public String getAllFieldsSql(String dbNames, String tableNames) {
        return null;
    }

    @Override
    public List<FieldEmptyExcelDto> getAllEmptyRateFields(String datalinkName, String dbNames, String schemas,
        Map<String, List<String>> fieldTableMap) {
        return null;
    }

    @Override
    public String getDriverClass() {
        return "org.apache.hive.jdbc.HiveDriver";
    }

    @Override
    public String getAllDatabasesSql() throws BusinessException {
        return "show databases";
    }

    @Override
    public String getAllSchemasSql() {
        return "show databases";
    }

    @Override
    public String getAllTablesSql() {
        String database = getDatabaseName();
        if (StringUtils.isNotBlank(database) && database.split(";").length > 1) {
            return "show tables in " + database.split(";")[0];
        }
        return "show tables in " + database;
    }

    @Override
    public String getURL(String hostname, String port, String databaseName) {
        try {
            hiveAuth(databaseName);
        } catch (IOException e) {
            log.error("Hive Auto failed, {}, hive url {}", e.getMessage(), databaseName);
        }
        return "jdbc:hive2://" + hostname + ":" + port + "/" + databaseName;
    }

    private void hiveAuth(String url) throws IOException {
        if (StringUtils.isBlank(url) || url.split(";").length < 3) {
            return;
        }
        String[] strings = url.split(";");

        String keytab = "";
        for (String s : strings) {
            if (s.startsWith("user.keytab")) {
                keytab = s;
                break;
            }
        }

        String path = keytab.split("=")[1];
        int len = path.length();
        path = path.substring(0, len - "user.keytab".length());

        HiveAuthUtil.auth(path + "krb5.conf");
    }
}
